#!/usr/bin/perl  -nl

# perl script to transform the output of 'iostat -xktc' into CSV
# suitable for analysis with R
#
#
# Author: Nathan Hubbard
# URL: http://code.google.com/p/n8foo
# -------------------------------------------------------------------------

next if /Linux/;        # Skip first line 
next if /^\s*$/;        # Skip blank lines 
next if /^\s/;          # Skip lines that start with a space
next if /^avg-cpu/;     # Skip blank lines 

# save time
if (/^(.*) (PM|AM)$/) {
        ($date,$time,$ampm) = split /\s+/;
        ($month,$day,$year) = split (/\//,$date);

        $timestamp = qq($year-$month-$day $time $ampm);
        next;
}
# Write header 
if (/^Device:/) {
        s/Device:/device/g;
        print join ",", ("time", split /\s+/) unless $header++;
        next;
}
# Write line
print join ",", $timestamp, split /\s+/;

